package com.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Vector;

import static android.content.ContentValues.TAG;

public class FileUtil {

    /**
     * 读取新数据保存到手机中
     *
     * @param str
     */
    public static void saveFile(String str, String fileName) {
        try {
            File file = new File(Environment.getExternalStorageDirectory()+"/xaland", fileName);
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(str.getBytes());
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static void saveInternalStorage(String fileName,String  data,Context context) {
        FileOutputStream out=null;
        BufferedWriter writer = null;
        try {
            out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    public static String readInternalStorage(String fileName,Context context) {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            in = context.openFileInput(fileName);
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
    public static Vector<String> GetInternalStorageFileName(Context context,String endsWith) {
        Vector<String> vecFile = new Vector<String>();
        String[] subFile =  context.fileList();

        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
                String filename = subFile[iFileLength];
                // 判断是否为txt结尾
                if (filename.trim().toLowerCase().endsWith(endsWith)) {
                    vecFile.add(filename);
                }
            }

        return vecFile;
    }

    public static Vector<String> GetFileName(String fileAbsolutePath) {
        Vector<String> vecFile = new Vector<String>();
        File file = new File(fileAbsolutePath);
        File[] subFile = file.listFiles();

        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
            // 判断是否为文件夹
            if (!subFile[iFileLength].isDirectory()) {
                String filename = subFile[iFileLength].getName();
                // 判断是否为txt结尾
                if (filename.trim().toLowerCase().endsWith(".txt")) {
                    vecFile.add(filename);
                }
            }
        }
        return vecFile;
    }

    /**
     * 删除已存储的文件
     */
    public static void deletefile(String fileName) {
        try {
            // 找到文件所在的路径并删除该文件
            File file = new File(Environment.getExternalStorageDirectory()+"/xaland", fileName);
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 读取文件里面的内容
     *
     * @return
     */
    public static String getFile(String fileName) {
        try {
            File file = new File(Environment.getExternalStorageDirectory()+"/xaland",fileName);
            FileInputStream fis = new FileInputStream(file);
            StringBuilder sb=new StringBuilder();
            byte[] buff=new byte[1024];
            int len=0;
            while ((len=fis.read(buff))!=-1){
                sb.append(new String(buff,0,len));
            }
            fis.close();
            Log.d(TAG, "getFile: "+sb.toString());
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

}
